'use strict';
app.controller('revenueSettingCtrl',['$scope','utilService','revenueSettingService','toaster','$modal', function($scope,utilService,revenueSettingService,toaster,$modal){
    $scope.revenueSetting = {};
    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'revenue/setting',
            params: $scope.revenueSetting
        }).then(function(result){
        });
    }
    $scope.query();

    //删除
    $scope.openDelete = function (id,type) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/sell/revenue_delete.html',
            controller: 'delRevenueSettingCtrl',
            scope: $scope,
            size:'sm',
            resolve: {
                data: function () {
                    var data = {};
                    data.id = id;
                    data.type=type;
                    return data;
                }
            }
        });
    }
}]);

//删除模态框Controller
app.controller('delRevenueSettingCtrl', ['$scope', '$modalInstance','$state','revenueSettingService','toaster','data',
    function($scope, $modalInstance,$state,revenueSettingService,toaster,data) {
        $scope.revenueSetting=data;

        $scope.AllowToDelete=true;

        //确认删除
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;
            revenueSettingService.delRevenueSetting($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToDelete=true;
                        $scope.query();
                        break;
                    case 683:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToDelete=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToDelete=true;
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


app.controller('revenueSettingAddCtrl',['$scope','$state','revenueSettingService','$stateParams','toaster',
    function($scope,$state,revenueSettingService,$stateParams,toaster) {
        //新增
        $scope.AllowToAdd=true;
        $scope.addRevenueSetting = function(){
            if(!$scope.AllowToAdd){
                return;
            }
            $scope.AllowToAdd=false;
            revenueSettingService.addRevenueSetting($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToAdd=true;
                        $state.go('app.revenueSetting');
                        break;
                    case 690:
                        toaster.pop('error', "操作失败", "类型已存在,不能重复添加");
                        $scope.AllowToAdd=true;
                        break;
                    default:
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToAdd=true;
                        break
                }
            });
        }
    }
]);

app.controller('revenueSettingEditCtrl',['$scope','revenueSettingService','$stateParams','toaster','$state',
    function($scope,revenueSettingService,$stateParams,toaster,$state) {
        $scope.revenueSetting = {
            id:$stateParams.id
        };
        //根据id查询
        revenueSettingService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.revenueSetting = result.data;
                    break;
                case 683:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });

        //修改
        $scope.AllowToEdit=true;
        $scope.revenueSettingEdit = function() {
            if(!$scope.AllowToEdit){
                return;
            }
            $scope.AllowToEdit=false;
            revenueSettingService.editRevenueSetting($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToEdit=true;
                        $state.go('app.revenueSetting');
                        break;
                    case 683:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToEdit=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToEdit=true;
                        break
                }
            });
        }
    }
]);



